package ouisseuk.gui;

import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import ouisseuk.model.SearchResult;
import ouisseuk.model.SearchResults;
import ouisseuk.tools.Discogs;

import com.flexycore.iphone.uikit.NSIndexPath;
import com.flexycore.iphone.uikit.UIAlertView;
import com.flexycore.iphone.uikit.UIFont;
import com.flexycore.iphone.uikit.UIImage;
import com.flexycore.iphone.uikit.UINavigationController;
import com.flexycore.iphone.uikit.UITableView;
import com.flexycore.iphone.uikit.UITableViewCell;
import com.flexycore.iphone.uikit.UITableViewCellAccessoryType;
import com.flexycore.iphone.uikit.UITableViewController;
import com.flexycore.iphone.uikit.UITableViewStyle;
import com.flexycore.iphone.uikit.UIView;
import com.flexycore.iphone.uikit.UIViewController;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
public class SearchResultsViewController extends UITableViewController {

	private UINavigationController nav;
	private SearchResults searchResults;

	public SearchResultsViewController(UINavigationController n, String searched) {
		super();
		initWithStyle(UITableViewStyle.UITableViewStyleGrouped);
		setTitle("Search Results");
		try {
			searchResults = Discogs.getSearchResults(searched);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			displayDialog("Incorrect response from Discogs server.");
		}
		nav = n;
	}

	private void displayDialog(String message) {
		UIAlertView alert = new UIAlertView();
		alert.setTitle("Error");
		alert.setMessage(message);
		alert.addButtonWithTitle("OK");
		alert.show();
	}

	@Override
	public int numberOfSectionsInTableView(UITableView tableView) {
		return 2;
	}

	@Override
	public int tableViewNumberOfRowsInSection(UITableView table, int section) {
		if (section == 0)
			return searchResults.getExactResults().size();
		else if (section == 1)
			return searchResults.getOtherResults().size();
		else
			return 0;
	}

	@Override
	public String tableViewTitleForHeaderInSection(UITableView tableView, int section) {
		if (section == 0)
			return "Exact Results:";
		else if (section == 1)
			return "Other Results:";
		else
			return "";
	}

	@Override
	public UITableViewCell tableViewCellForRowAtIndexPath(UITableView tableView, NSIndexPath indexPath) {
		UITableViewCell cell = new UITableViewCell();
		cell.init();
		cell.setAccessoryType(UITableViewCellAccessoryType.UITableViewCellAccessoryDisclosureIndicator);
		cell.setIndentationLevel(0);
		UIImage icon;

		List<SearchResult> results;

		if (indexPath.section() == 0)
			results = searchResults.getExactResults();
		else
			results = searchResults.getOtherResults();

		SearchResult searchResult = results.get(indexPath.row());
		SearchResult.Type type = searchResult.getType();
		cell.setText(searchResult.getTitle());

		switch (type) {
		case artist:
			icon = UIImage.imageNamed("GarageBand_32.png");
			break;
		default:
			icon = UIImage.imageNamed("Vinyle-32.png");
		}

		cell.setImage(icon);
		cell.setSelectedImage(icon);
		cell.setFont(UIFont.fontWithNameSize("Arial", 14));

		return cell;
	}

	@Override
	public void tableViewDidSelectRowAtIndexPath(UITableView tableView, NSIndexPath indexPath) {
		UIViewController viewController = null;

		viewController = new UIViewController().init();
		viewController.setTitle("Details");

		List<SearchResult> searchResultList = null;
		int section = indexPath.section();

		if (section == 0)
			searchResultList = searchResults.getExactResults();
		if (section == 1)
			searchResultList = searchResults.getOtherResults();

		SearchResult searchResult = searchResultList.get(indexPath.row());
		UIView view = null;
		SearchResult.Type type = searchResult.getType();
		switch (type) {
		case release:
			view = new ReleaseView(searchResult.getUri(), nav);

			break;
		case artist:
			view = new ArtistView(searchResult, nav);

			break;
		}

		viewController.setView(view);

		nav.pushViewControllerAnimated(viewController, true);
	}

}
